package fr.afcepf.atod18.commande.web;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import fr.afcepf.atod18.client.entity.Client;
import fr.afcepf.atod18.client.service.ServiceClient;
import fr.afcepf.atod18.commande.entity.Commande;
import fr.afcepf.atod18.commande.entity.Panier;
import fr.afcepf.atod18.commande.service.IGestionCommande;
import fr.afcepf.atod18.commande.service.IGestionPanier;


@ManagedBean
@SessionScoped
public class CommandeMB {
	@ManagedProperty(value="#{gestionCommande}")
	private IGestionCommande gestionCommande;
	@ManagedProperty(value="#{gestionPanier}")
	private IGestionPanier gestionPanier;
	@ManagedProperty(value="#{gestionClient}")
	private ServiceClient gestionClient;

	private String login;
	private Integer idSelPanier;
	private Commande commande;
	private Panier panier;
	private boolean creation;
	
	public IGestionPanier getGestionPanier() {
		return gestionPanier;
	}
	public Panier getPanier() {
		return panier;
	}
	public void setPanier(Panier panier) {
		this.panier = panier;
	}
	public void setGestionPanier(IGestionPanier gestionPanier) {
		this.gestionPanier = gestionPanier;
	}
	public ServiceClient getGestionClient() {
		return gestionClient;
	}
	public void setGestionClient(ServiceClient gestionClient) {
		this.gestionClient = gestionClient;
	}
	public IGestionCommande getGestionCommande() {
		return gestionCommande;
	}
	public void setGestionCommande(IGestionCommande gestionCommande) {
		this.gestionCommande = gestionCommande;
	}
	public String getLogin() {
		//login="toto"; 
		Client client=gestionClient.chercherClient(login);
		System.out.println("client cde " + client);
		if (client==null){
			System.out.println("attention ajout");
			client=new Client();
			client.setLogin(login);
			client.setNom("MARTIN");
			client.setPrenom("Pierre");
			System.out.println("ajout client" + client);
			gestionClient.ajoutClient(client);
			System.out.println(" client ajouté " + client.getId());
		}
		commande.setClient(client);
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public Integer getIdSelPanier() {
		//idSelPanier=1;
		System.out.println("id panier cde" +idSelPanier);
		List<Panier> paniers = gestionPanier.rechercherParId(idSelPanier);
		 panier= paniers.get(0);
		System.out.println("panier cde" + panier);
		commande.setTotalCommande(panier.getTotalPanier());
		
		return idSelPanier;
	}
	public void setIdSelPanier(Integer idSelPanier) {
		this.idSelPanier = idSelPanier;
	}
	public Commande getCommande() {

		return commande;
	}
	public void setCommande(Commande commande) {

		this.commande = commande;
	}
	
	public CommandeMB() {
		super();
		System.out.println("construction MB");
	}
	@PostConstruct
	public void chargeCommande(){
		commande= new Commande();
		GregorianCalendar calendar1=new GregorianCalendar();
		calendar1.setTime(new Date());
		commande.setDateCommandeCalendar(calendar1);
		GregorianCalendar calendar2=new GregorianCalendar();
		calendar2.setTime(new Date());
		commande.setDateDemandeLivraison(calendar2);

	}
	
	public String doCommande(){
		System.out.println("attention confirmation commande");
		if (! creation){gestionCommande.ajouter(commande);creation=true;}
		return "commandeOK" ;
		}
		
}
